<template>
  <div class="app-container">
    <div class="">
      <div style="display: flex" :gutter="10">
        <div style="flex: 1">
          <el-button @click="gobackWay" type="text" icon="el-icon-arrow-left"
            style="color: rgba(0, 0, 0, 0.85)">返回</el-button>
          <el-divider direction="vertical"></el-divider>
          <span>{{ id ? "修改爱心助农活动" : "添加爱心助农活动" }}</span>
        </div>
      </div>
    </div>
    <div class="" v-loading="loading" style="height: calc(85vh - 80px); margin-top: 20px">
      <div style="position: relative; display: flex">
        <div style="flex: 1; min-width: 500px">
          <el-scrollbar wrap-style="overflow-x:hidden;" style="height: calc(73vh - 80px)">
            <el-form :model="basicForm" :rules="basicRules" ref="basicForm" label-width="110px" class="demo-ruleForm">
              <el-col :span="23" class="splitTitle">
                <div class="split">活动信息</div>
              </el-col>
              <el-col :span="15">
                <el-form-item label="活动名称" prop="activity_name">
                  <el-input placeholder="请输入活动名称" v-model="basicForm.activity_name" maxlength="20"
                    show-word-limit></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="活动轮播" prop="images">
                  <logo-upload :file-list="images" :show-file-list="true" accept="image/jpeg, image/png"
                    @uploadCallback="carouselBack" />
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="时间段" prop="domeTime">
                  <el-date-picker @change="domeTimeWay" style="width: 100%" value-format="yyyy-MM-dd" v-model="domeTime"
                    type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="活动排序" prop="sort">
                  <el-input placeholder="请输入活动排序" v-model="basicForm.sort" maxlength="20" show-word-limit
                    onkeyup="value=value.replace(/\D/g,'')"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="省市区" prop="cascaderRegList">
                  <el-cascader @change="cascaderReWay" v-model="cascaderRegList" style="width: 100%"
                    :options="regionList" collapse-tags :props="{
                      multiple: true,
                      value: 'id',
                      label: 'name',
                      children: 'children',
                      checkStrictly: true,
                    }" clearable></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="活动说明" prop="explain">
                  <el-input placeholder="请输入活动说明" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"
                    v-model="basicForm.explain" maxlength="200" show-word-limit></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="15">
                <el-form-item label="备注" prop="remark">
                  <el-input placeholder="请输入备注" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }"
                    v-model="basicForm.remark" maxlength="200" show-word-limit></el-input>
                </el-form-item>
              </el-col>
            </el-form>
          </el-scrollbar>
        </div>

        <div style="flex: 1; min-width: 480px; margin-left: 20px">
          <el-scrollbar wrap-style="overflow-x:hidden;" style="height: calc(73vh - 80px)">
            <el-form :model="activityRule" :rules="ruleRules" ref="activityRule" label-width="110px"
              class="demo-ruleForm">
              <el-col :span="23" class="splitTitle">
                <div class="split">活动规则</div>
              </el-col>
              <el-col :span="15">
                <el-form-item label="参与类型" prop="p_goods_type">
                  <el-radio-group @change="p_goods_typeWay" v-model="activityRule.p_goods_type" size="mini">
                    <el-radio :label="1">商户商品</el-radio>
                    <el-radio :label="2">商户</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="15" v-if="activityRule.p_goods_type == 1">
                <el-form-item label="参与活动商品" prop="goods_ids">
                  <el-select multiple @change="merchantGoods_idsWay" v-model="merchantGoods_ids" style="width: 100%"
                    filterable remote clearable reserve-keyword placeholder="关键字查询(ID、名称、商户)"
                    :remote-method="getMerchantGoodsListWay" :loading="getSearchLoading">
                    <el-option v-for="item in merchantGoodsList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="15" v-if="activityRule.p_goods_type == 2">
                <el-form-item label="参与活动商户" prop="goods_ids">
                  <el-select @change="merchant_idsWay" multiple v-model="merchant_ids" style="width: 100%" filterable
                    remote clearable reserve-keyword placeholder="关键字查询(id、名称、手机号)" :remote-method="getMerchantListWay"
                    :loading="getSearchLoading">
                    <el-option v-for="item in merchantList" :key="item.id" :label="item.name" :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="20">
                <el-form-item label="参与用户" prop="p_user_type">
                  <el-radio-group @change="p_user_typeWay" v-model="activityRule.p_user_type" size="mini">
                    <el-radio :label="1">全部</el-radio>
                    <el-radio :label="2">指定用户</el-radio>
                    <el-radio :label="3">指定用户等级</el-radio>
                    <el-radio :label="4">指定用户标签</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="15" v-if="activityRule.p_user_type == 2">
                <el-form-item label="指定用户" prop="user_ids">
                  <el-select multiple @change="user_idsWay" v-model="user_ids" style="width: 100%" filterable remote
                    clearable reserve-keyword placeholder="关键字查询(名称、ID、手机号)" :remote-method="getUserListWay"
                    :loading="getSearchLoading">
                    <el-option v-for="item in userList" :key="item.id" :label="item.nickname + '(' + item.mobile + ')'"
                      :value="item.id" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="15" v-if="activityRule.p_user_type == 3">
                <el-form-item label="指定用户等级" prop="user_ids">
                  <el-select clearable multiple @change="level_idWay" v-model="level_id" placeholder="请选择等级">
                    <el-option v-for="item in levelList" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="15" v-if="activityRule.p_user_type == 4">
                <el-form-item label="指定用户标签" prop="user_ids">
                  <el-select @change="related_user_tagWay" v-model="related_user_tag" multiple clearable
                    placeholder="请选择用户标签">
                    <el-option-group v-for="group in gradeList" :key="group.label" :label="group.category_name">
                      <el-option v-for="item in group.child" :key="item.id" :label="item.tag_name" :value="item.id" />
                    </el-option-group>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22" v-for="(item, index) in activityRule.rule" :key="index">
                <el-col :span="17">
                  <el-col :span="24">
                    <el-form-item label="消费额最小值" :prop="'rule.' + index + '.section_start'">
                      <el-input @input="ection_startWay(item)" :style="
                          item.section_startBool
                            ? 'border-color: #ff4949 !important;'
                            : ''
                        " placeholder="最小值" v-model.trim="item.section_start" maxlength="6" show-word-limit></el-input>
                      <span style="
                          color: #ff4949;
                          font-size: 12px;
                          line-height: 1;
                          padding-top: 4px;
                          position: absolute;
                          top: 100%;
                          left: 0;
                        " v-if="item.section_startBool">请输入整数,且不能大于最大值</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="消费额最大值" :prop="'rule.' + index + '.section_end'">
                      <el-input @input="section_endWay(item)" :style="
                          item.section_endBool
                            ? 'border-color: #ff4949 !important;'
                            : ''
                        " placeholder="最大值" v-model.trim="item.section_end" maxlength="6"
                        show-word-limit></el-input>
                      <span style="
                          color: #ff4949;
                          font-size: 12px;
                          line-height: 1;
                          padding-top: 4px;
                          position: absolute;
                          top: 100%;
                          left: 0;
                        " v-if="item.section_endBool">请输入整数,且不能小于最小值</span>
                    </el-form-item>
                  </el-col>

                  <el-col :span="24">
                    <el-form-item label="送积分" :prop="'rule.' + index + '.integral'">
                      <el-input @input="integralWay(item)" :style="
                          item.integralBool
                            ? 'border-color: #ff4949 !important;'
                            : ''
                        " placeholder="请输入送积分数量" v-model="item.integral" maxlength="7" show-word-limit></el-input>
                      <span style="
                          color: #ff4949;
                          font-size: 12px;
                          line-height: 1;
                          padding-top: 4px;
                          position: absolute;
                          top: 100%;
                          left: 0;
                        " v-if="item.integralBool">请输入整数</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="24">
                    <el-form-item label="关联平台商品" :prop="'rule.' + index + '.goodsIdsList'">
                      <el-select multiple :style="
                          item.goodsIdsBool
                            ? 'border-color: #ff4949 !important;'
                            : ''
                        " @change="goodsIdsWay(item)" v-model="item.goodsIdsList" style="width: 100%" filterable remote
                        clearable reserve-keyword placeholder="关键字查询(名称、ID、编码)" :remote-method="getplatformGoodsListWay"
                        :loading="getSearchLoading">
                        <el-option v-for="item in platformGoodsList" :key="item.id" :label="item.name"
                          :value="item.id" />
                      </el-select>
                      <span style="
                          color: #ff4949;
                          font-size: 12px;
                          line-height: 1;
                          padding-top: 4px;
                          position: absolute;
                          top: 100%;
                          left: 0;
                        " v-if="item.goodsIdsBool">请选择平台商品</span>
                    </el-form-item>
                  </el-col>
                </el-col>
                <el-col :span="17" style="text-align: right; margin-bottom: 20px">
                  <el-button @click="addRuleWay" type="primary" icon="el-icon-plus" circle></el-button>
                  <el-button @click="delRuleWay(item)" v-if="activityRule.rule.length > 1" type="danger"
                    icon="el-icon-minus" circle></el-button></el-col>
              </el-col>
            </el-form>
          </el-scrollbar>
        </div>
      </div>
      <div style="margin-top: 30px; text-align: center">
        <el-button type="primary" @click="submitWay">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    getRegionList,
    getUserList,
    getTagCategoryAll,
    getgradeList,
  } from "@/api/user/index";
  import {
    SaveLoveFarmer,
    detailFarmerGoods,
    getMerchantGoodsList,
  } from "@/api/activity/agriculture";
  import {
    getActivityGoodsList,
    getMerchantAllList,
    getPlatformGoodsList,
  } from "@/api/index";
  import LogoUpload from "@/components/FileUpload";
  export default {
    components: {
      LogoUpload
    },
    data() {
      return {
        id: "",
        loading: true,
        regionList: [], // 获取的省市区
        merchantGoodsList: [], // 商户商品列表
        merchantList: [], // 商户列表
        userList: [], // 用户列表
        levelList: [], // 用户等级列表
        gradeList: [], // 用户标签
        platformGoodsList: [], // 平台商品列表
        basicForm: {
          activity_name: "",
          image: "",
          start_time: "",
          end_time: "",
          explain: "",
          sort: "",
          remark: "",
          area_ids_json: "",
        }, // 活动基本信息
        basicRules: {
          activity_name: [{
            required: true,
            validator: (rule, value, callback) => {
              let req = /[^0-9a-zA-Z\u4e00-\u9fa5]/g;
              if (!value) {
                callback(new Error("请输入活动名称"));
              } else {
                if (req.test(value)) {
                  callback(new Error("活动名称中不能存在特殊字符"));
                } else {
                  callback();
                }
              }
            },
            trigger: "change",
          }, ],
          images: [{
            required: true,
            validator: (rule, value, callback) => {
              if (this.images.length == 0) {
                return callback(new Error("请上传轮播图片"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          }, ],
          domeTime: [{
            required: true,
            validator: (rule, value, callback) => {
              if (this.domeTime.length == 0) {
                return callback(new Error("请选择活动的时间段"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          }, ],
          sort: [{
            required: true,
            validator: (rule, value, callback) => {
              if (value == "") {
                return callback(new Error("请输入序号,序号越大越靠前"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          }, ],
          explain: [{
            required: true,
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("请输入活动说明"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          }, ],
        }, // 活动基本信息校验
        images: [], // 活动轮播图
        domeTime: [], // 时间段
        merchantGoods_ids: [], // 选中的参与活动的商品
        merchant_ids: [], // 选中的参与活动的商户
        cascaderRegList: [], // 选中的省市区
        related_user_tag: [], // 选中的标签
        level_id: [], // 选中的等级
        user_ids: [], // 选中的用户
        reqNumber: /\D/g,
        getSearchLoading: false,
        activityRule: {
          p_goods_type: 1,
          goods_ids: "",
          p_user_type: 1,
          user_ids: 0,
          rule: [{
            id: 0,
            section_start: "",
            section_startBool: true,
            section_end: "",
            section_endBool: true,
            integral: "",
            integralBool: true,
            goods_ids: "",
            goodsIdsList: [],
            goodsIdsBool: true,
          }, ],
        }, // 活动规则
        ruleRules: {
          p_goods_type: [{
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          }, ],
          goods_ids: [{
            required: true,
            validator: (rule, value, callback) => {
              if (this.activityRule == "") {
                if (activityRule.p_goods_type == 1) {
                  callback(new Error("请选择参与活动商品称"));
                } else {
                  callback(new Error("请选择参与活动商户"));
                }
              } else {
                callback();
              }
            },
            trigger: "change",
          }, ],
          p_user_type: [{
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          }, ],
          user_ids: [{
            required: true,
            validator: (rule, value, callback) => {
              if (
                this.activityRule.p_user_type !== 1 &&
                this.activityRule.user_ids === ""
              ) {
                if (this.activityRule.p_user_type == 2) {
                  callback(new Error("请选择指定用户"));
                } else if (this.activityRule.p_user_type == 3) {
                  callback(new Error("请选择指定用户等级"));
                } else {
                  callback(new Error("请选择指定用户标签"));
                }
              } else {
                callback();
              }
            },
            trigger: "change",
          }, ],
          section_start: [{
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          }, ],
          section_end: [{
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          }, ],
          integral: [{
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          }, ],
          goodsIdsList: [{
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          }, ],
        }, // 互动规则校验
        backupsForm: {}, // 备份获取的数据
      };
    },
    created() {
      var _this = this;
      const p1 = new Promise((req, rej) => {
        getRegionList().then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            _this.$messages.error(res.msg);
            rej();
            return;
          }
          _this.regionList = res.data;
          req();
        });
      });
      const p2 = new Promise((req, rej) => {
        getMerchantGoodsList().then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            _this.$messages.error(res.msg);
            rej();
            return;
          }
          _this.merchantGoodsList = res.data;
          req();
        });
      });
      const p3 = new Promise((req, rej) => {
        getMerchantAllList({
          keyword: ""
        }).then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            _this.$messages.error(res.msg);
            rej();
            return;
          }
          _this.merchantList = res.data;
          req();
        });
      });
      const p4 = new Promise((req, rej) => {
        getUserList({
          search_name: "",
          search_type: 1,
          page: 1,
          count: 10
        }).then(
          (res) => {
            if (res.code !== 200 || res.status !== 1) {
              _this.$messages.error(res.msg);
              rej();
              return;
            }
            _this.userList = res.data.list;
            req();
          }
        );
      });
      const p5 = new Promise((req, rej) => {
        getgradeList().then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            _this.$messages.error(res.msg);
            rej();
            return;
          }
          _this.levelList = res.data.list;
          req();
        });
      });
      const p6 = new Promise((req, rej) => {
        getTagCategoryAll().then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            _this.$messages.error(res.msg);
            rej();
            return;
          }
          _this.gradeList = res.data;
          req();
        });
      });
      const p7 = new Promise((req, rej) => {
        getPlatformGoodsList().then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            _this.$messages.error(res.msg);
            rej();
            return;
          }
          _this.platformGoodsList = res.data.list;
          req();
        });
      });
      var arr = [];
      arr = [p1, p2, p3, p4, p5, p6, p7];
      if (_this.$route.query.id) {
        _this.id = _this.$route.query.id;
        const p8 = new Promise((req, rej) => {
          detailFarmerGoods({
            id: _this.id
          }).then((res) => {
            if (res.code !== 200 || res.status !== 1) {
              _this.$messages.error(res.msg);
              rej();
              return;
            }
            console.log(res.data);
            _this.backupsForm = res.data;
            req();
          });
        });
        arr = [p1, p2, p3, p4, p5, p6, p7, p8];
      }
      Promise.all(arr)
        .then(() => {
          if (_this.id) {
            _this.getBackupsForm();
          } else {
            _this.loading = false;
          }
        })
        .catch(() => {
          _this.loading = false;
        });
    },
    methods: {
      praint(value) {
        return Number(value)
      },
      // 获取修改数据
      getBackupsForm() {
        this.basicForm.activity_name = this.backupsForm.activity_name;
        this.images = [];
        var arr = this.backupsForm.image.split(",");
        arr.forEach((item) => {
          this.images.push({
            url: item,
          });
        });
        this.basicForm.image = this.backupsForm.image;
        this.domeTime = [this.backupsForm.start_time, this.backupsForm.end_time];
        this.basicForm.start_time = this.backupsForm.start_time;
        this.basicForm.end_time = this.backupsForm.end_time;
        this.basicForm.sort = this.backupsForm.sort;
        this.cascaderRegList = JSON.parse(this.backupsForm.area_ids_json);
        this.basicForm.area_ids_json = this.backupsForm.area_ids_json;
        this.basicForm.explain = this.backupsForm.explain;
        this.basicForm.remark = this.backupsForm.remark;
        this.activityRule.p_goods_type = this.backupsForm.p_goods_type;
        this.backupsForm.merchant.forEach((item) => {
          if (this.backupsForm.p_goods_type == 1) {
            this.merchantGoods_ids.push(item.id);
          } else {
            this.merchant_ids.push(item.id);
          }
        });
        if (this.backupsForm.p_goods_type == 1) {
          this.merchantGoodsList.push(...this.backupsForm.merchant);
          let forData1 = [];
          for (let i = 0; i < this.merchantGoodsList.length; i++) {
            if (!forData1.some((e) => e.id == this.merchantGoodsList[i].id))
              forData1.push(this.merchantGoodsList[i]);
          }
          this.merchantGoodsList = forData1;
          this.activityRule.goods_ids = this.merchantGoods_ids.toString();
        } else {
          this.merchantList.push(...this.backupsForm.merchant);
          let forData2 = [];
          for (let i = 0; i < this.merchantList.length; i++) {
            if (!forData2.some((e) => e.id == this.merchantList[i].id))
              forData2.push(this.merchantList[i]);
          }
          this.merchantList = forData2;
          this.activityRule.goods_ids = this.merchant_ids.toString();
        }

        this.activityRule.p_user_type = this.backupsForm.p_user_type;
        if (this.backupsForm.p_user_type == 1) {
          this.activityRule.user_ids = 0;
        } else if (this.backupsForm.p_user_type == 2) {
          this.activityRule.user_ids = [];
          this.userList.push(...this.backupsForm.userInfo);
          let forData3 = [];
          for (let i = 0; i < this.userList.length; i++) {
            if (!forData3.some((e) => e.id == this.userList[i].id))
              forData3.push(this.userList[i]);
          }
          this.userList = forData3;
          this.backupsForm.userInfo.forEach((item) => {
            this.user_ids.push(item.id);
          });
          this.activityRule.user_ids = this.user_ids.toString();
        } else if (this.backupsForm.p_user_type == 3) {
          this.activityRule.user_ids = [];
          this.level_id = [];
          this.backupsForm.userInfo.forEach((item) => {
            this.level_id.push(item.id);
          });
          this.activityRule.user_ids = this.level_id.toString();
        } else {
          this.activityRule.user_ids = [];
          this.related_user_tag = [];
          this.backupsForm.userInfo.forEach((item) => {
            this.related_user_tag.push(item.id);
          });
          this.activityRule.user_ids = this.related_user_tag.toString();
        }
        this.activityRule.rule = [];
        this.backupsForm.ruleList.forEach((item) => {
          item.section_end = Number(item.section_end)
          item.section_start = Number(item.section_start)
          item.integral = Number(item.integral)
          item.section_startBool = false;
          item.section_endBool = false;
          item.integralBool = false;
          item.goodsIdsBool = false;
          item.goodsIdsList = [];
          item.goods_list.forEach((items, index) => {
            item.goodsIdsList.push(items.id);
          });
          item.goods_ids = item.goodsIdsList.toString();
          this.platformGoodsList.push(...item.goods_list);
          this.activityRule.rule.push(item);
        });
        let forData = [];
        for (let i = 0; i < this.platformGoodsList.length; i++) {
          if (!forData.some((e) => e.id == this.platformGoodsList[i].id))
            forData.push(this.platformGoodsList[i]);
        }
        this.platformGoodsList = forData;
        this.loading = false;
      },
      // 返回本地团商品列表
      gobackWay() {
        this.$router.push("/activity/agriculture");
      },
      // 上传活动轮播的回调
      carouselBack(files) {
        this.images = files;
        var arr = [];
        this.images.forEach((item) => {
          arr.push(item.url);
        });
        this.basicForm.image = arr.toString();
      },
      // 时间段发生改变执行
      domeTimeWay(val) {
        if (val.length !== 0) {
          this.basicForm.start_time = val[0];
          this.basicForm.end_time = val[1];
        } else {
          this.basicForm.start_time = "";
          this.basicForm.end_time = "";
        }
      },
      // 省市区发生改变执行
      cascaderReWay(val) {
        if (val.length !== 0) {
          this.basicForm.area_ids_json = JSON.stringify(val);
        } else {
          this.basicForm.area_ids_json = "";
        }
      },
      p_goods_typeWay() {
        this.activityRule.goods_ids = "";
      },
      // 动态获取商户列表
      getMerchantListWay(query) {
        this.getSearchLoading = true;
        getMerchantAllList({
          keywords: query
        }).then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            this.$messages.error(res.msg);
            this.getSearchLoading = false;
            return;
          }
          this.merchantList = res.data;
          this.getSearchLoading = false;
        });
      },
      merchantGoods_idsWay(val) {
        if (val.length !== 0) {
          this.activityRule.goods_ids = val.toString();
        } else {
          this.activityRule.goods_ids = "";
        }
      },
      // 动态获取商户商品列表
      getMerchantGoodsListWay(query) {
        this.getSearchLoading = true;
        getActivityGoodsList({
          action: "love",
          keyword: query
        }).then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            this.$messages.error(res.msg);
            this.getSearchLoading = false;
            return;
          }
          this.merchantGoodsList = res.data.list;
          this.getSearchLoading = false;
        });
      },
      merchant_idsWay(val) {
        if (val.length !== 0) {
          this.activityRule.goods_ids = val.toString();
        } else {
          this.activityRule.goods_ids = "";
        }
      },
      p_user_typeWay(val) {
        if (val === 1) {
          this.activityRule.user_ids = 0;
        } else {
          this.activityRule.user_ids = "";
        }
      },
      // 动态获取用户列表
      getUserListWay(query) {
        this.getSearchLoading = true;
        getUserList({
          search_name: query,
          search_type: 1
        }).then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            this.$messages.error(res.msg);
            this.getSearchLoading = false;
            return;
          }
          this.userList = res.data.list;
          this.getSearchLoading = false;
        });
      },
      user_idsWay(val) {
        if (val.length !== 0) {
          this.activityRule.user_ids = val.toString();
        } else {
          this.activityRule.user_ids = "";
        }
      },
      level_idWay(val) {
        if (val.length !== 0) {
          this.activityRule.user_ids = val.toString();
        } else {
          this.activityRule.user_ids = "";
        }
      },
      related_user_tagWay(val) {
        if (val.length !== 0) {
          this.activityRule.user_ids = val.toString();
        } else {
          this.activityRule.user_ids = "";
        }
      },
      getplatformGoodsListWay(query) {
        getPlatformGoodsList({
          keywords: query
        }).then((res) => {
          if (res.code !== 200 || res.status !== 1) {
            this.$messages.error(res.msg);
            return;
          }
          this.platformGoodsList = res.data.list;
        });
      },
      ection_startWay(item) {
        const req = /\D/g;
        item.section_startBool = req.test(item.section_start);
        if (!item.section_startBool && item.section_end) {
          if (Number(item.section_end) < Number(item.section_start)) {
            item.section_startBool = true;
          }
        }
        this.$forceUpdate();
      },
      section_endWay(item) {
        const req = /\D/g;
        item.section_endBool = req.test(item.section_end);
        if (!item.section_endBool && item.section_start) {
          if (Number(item.section_end) < Number(item.section_start)) {
            item.section_endBool = true;
          }
        }
        this.$forceUpdate();
      },
      integralWay(item) {
        const req = /\D/g;
        item.integralBool = req.test(item.integral);
        this.$forceUpdate();
      },
      goodsIdsWay(item) {
        if (item.goodsIdsList.length == 0) {
          item.goodsIdsBool = true;
          item.goods_ids = "";
        } else {
          item.goodsIdsBool = false;
          item.goods_ids = item.goodsIdsList.toString();
        }
        this.$forceUpdate();
      },
      // 添加更多规则
      addRuleWay() {
        this.activityRule.rule.push({
          id: this.activityRule.rule.length + "" + new Date().getTime(),
          section_start: "",
          section_startBool: true,
          section_end: "",
          section_endBool: true,
          integral: "",
          integralBool: true,
          goods_ids: "",
          goodsIdsList: [],
          goodsIdsBool: true,
        });
      },
      // 删除规则
      delRuleWay(row) {
        var arr = [];
        this.activityRule.rule.forEach((item) => {
          if (item.id !== row.id) {
            arr.push(item);
          }
        });
        this.activityRule.rule = arr;
      },
      submitWay() {
        var _this = this;
        const p1 = new Promise((req, rej) => {
          _this.$refs.basicForm.validate((valid) => {
            if (valid) {
              req();
            } else {
              rej();
              return false;
            }
          });
        });
        const p2 = new Promise((req, rej) => {
          _this.$refs.activityRule.validate((valid) => {
            if (valid) {
              req();
            } else {
              rej();
              return false;
            }
          });
        });
        const p3 = new Promise((req, rej) => {
          _this.activityRule.rule.some((item, index) => {
            if (item.section_startBool) {
              rej();
              return ture;
            } else if (item.section_endBool) {
              rej();
              return ture;
            } else if (item.integralBool) {
              rej();
              return ture;
            } else if (item.goodsIdsBool) {
              rej();
              return ture;
            }
          });
          req();
        });
        Promise.all([p1, p2, p3])
          .then(() => {
            var obj = {
              id: this.id,
              ..._this.basicForm,
              ..._this.activityRule,
            };
            obj.rule = JSON.stringify(obj.rule);
            _this.loading = true;
            SaveLoveFarmer(obj).then((res) => {
              if (res.code !== 200 || res.status !== 1) {
                _this.$messages.error(res.msg);
                _this.loading = false;
                return;
              }
              _this.gobackWay();
            });
          })
          .catch(() => {
            console.log("有没填的");
          });
      },
    },
  };
</script>

<style>
  .el-upload--picture-card {
    width: 90px !important;
    height: 90px !important;
    line-height: 90px;
  }

  .el-upload-list--picture-card .el-upload-list__item-actions {
    width: 90px !important;
    height: 90px !important;
  }

  .el-upload-list--picture-card .el-upload-list__item-thumbnail {
    width: 90px !important;
    height: 90px !important;
  }

  .el-upload-list--picture-card .el-upload-list__item {
    width: 90px !important;
    position: sticky;
    height: 90px !important;
  }
</style>
